/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.JkyContributionAttMapper;
import cn.qidisoft.edu.hzjt.dao.JkyContributionMapper;
import cn.qidisoft.edu.hzjt.model.JkyContribution;
import cn.qidisoft.edu.hzjt.model.JkyContributionAtt;
import cn.qidisoft.edu.hzjt.service.IJkyContributionService;
import cn.qidisoft.edu.hzjt.utils.LogType;


/**
 * 
 * @author hxq
 * @version $Id: LoggerServiceImpl.java, v 0.1 2018年9月18日 下午5:55:55 hxq Exp $
 */
@Service("jkyContributionService")
public class JkyContributionServiceImpl implements IJkyContributionService {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);
  @Autowired
  private JkyContributionMapper jkyContributionMapper;
  @Autowired
  private JkyContributionAttMapper jkyContributionAttMapper;

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#deleteByPK(java.lang.Integer)
   */
  @Override
  @Transactional
  public int deleteByPK(Integer tgid) {
    try {
      // 删除前，需要判断附件表是否有记录，有则先删除附件表，在删除主表
      jkyContributionAttMapper.deleteByParentIdPK(tgid);
      return jkyContributionMapper.deleteByPK(tgid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(tgid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#insert(cn.qidisoft.edu.hzjt.model.JkyContribution)
   */
  @Override
  @Transactional
  public int insert(JkyContribution record) {
    try {
      int num = 0;
      jkyContributionMapper.insert(record);
      Integer tgid = record.getTgid();
      num = tgid;
      if (tgid >= 1) {
        if (record != null && record.getJkyContributionAttList() != null
            && !record.getJkyContributionAttList().isEmpty()) {
          num = 0;
          for (JkyContributionAtt jkyContributionAtt : record.getJkyContributionAttList()) {
            jkyContributionAtt.setParentid(tgid);
            jkyContributionAttMapper.insert(jkyContributionAtt);
            num++;
          }
        }
      }
      return num >= 1 ? record.getTgid() : 0;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("insert [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(record, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#queryByPK(java.lang.Integer)
   */
  @Override
  public JkyContribution queryByPK(Integer tgid) {
    try {
      return jkyContributionMapper.queryByPK(tgid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(tgid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }

  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#updateByPK(cn.qidisoft.edu.hzjt.model.JkyContribution)
   */
  @Override
  @Transactional
  public int updateByPK(JkyContribution record) {
    try {
      // 修改主表信息
      int num = jkyContributionMapper.updateByPK(record);
      // 修改子表信息
      if (record != null && record.getJkyContributionAttList() != null
          && !record.getJkyContributionAttList().isEmpty()) {
        for (JkyContributionAtt att : record.getJkyContributionAttList()) {
          att.setParentid(record.getTgid());
          num = jkyContributionAttMapper.insert(att);
        }
      }
      return num;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("updateByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(record, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#queryJkyContributionPageList(cn.qidisoft.edu.hzjt.model.JkyContribution,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<JkyContribution> queryJkyContributionPageList(
      JkyContribution jkyContribution, Page page) {
    try {
      int c = jkyContributionMapper.queryJkyContributionPageCount(jkyContribution);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<JkyContribution> pageHolder =
          new PaginationBean<JkyContribution>(page, totalRecords);
      List<JkyContribution> resultList =
          jkyContributionMapper.queryJkyContributionPageList(jkyContribution,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryJkyContributionPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(
              jkyContribution + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#deleteForumContributions(java.lang.String[])
   */
  @Override
  @Transactional
  public List<JkyContributionAtt> deleteForumContributions(String[] idArrs) {
    try {
      JkyContributionAtt contributionAtt = new JkyContributionAtt();
      contributionAtt.setIds(idArrs);
      List<JkyContributionAtt> jkyContributionAttList =
          jkyContributionAttMapper.queryContributionsAttList(contributionAtt);
      // 先删除子表信息
      int num = 0;
      if (!jkyContributionAttList.isEmpty()) {
        num = jkyContributionAttMapper.deleteContributionAtts(contributionAtt);
        if (num >= 1) {
          num = jkyContributionMapper.deleteJkyContributionList(contributionAtt);
        }
      } else {
        num = jkyContributionMapper.deleteJkyContributionList(contributionAtt);
      }
      return num == 0 ? null : jkyContributionAttList;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteForumContributions [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(idArrs, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }


  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#queryJkyContributionManagerPageList(cn.qidisoft.edu.hzjt.model.JkyContribution,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<JkyContribution> queryJkyContributionManagerPageList(
      JkyContribution jkyContribution, Page page) {
    try {
      int c = jkyContributionMapper.queryJkyContributionManagerPageCount(jkyContribution);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<JkyContribution> pageHolder =
          new PaginationBean<JkyContribution>(page, totalRecords);
      List<JkyContribution> resultList =
          jkyContributionMapper.queryJkyContributionManagerPageList(jkyContribution,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryJkyContributionManagerPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(
              jkyContribution + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#exportJkyContributionManagerPageList(cn.qidisoft.edu.hzjt.model.JkyContribution)
   */
  @Override
  public List<JkyContribution> exportJkyContributionManagerPageList(
      JkyContribution jkyContribution) {
    try {
      return jkyContributionMapper.exportJkyContributionManagerPageList(jkyContribution);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "exportJkyContributionManagerPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(jkyContribution, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#queryScientificJobPageList(cn.qidisoft.edu.hzjt.model.JkyContribution,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<JkyContribution> queryScientificJobPageList(JkyContribution jkyContribution,
      Page page) {
    try {
      int c = jkyContributionMapper.queryScientificJobPageCount(jkyContribution);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<JkyContribution> pageHolder =
          new PaginationBean<JkyContribution>(page, totalRecords);
      List<JkyContribution> resultList =
          jkyContributionMapper.queryScientificJobPageList(jkyContribution,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryScientificJobPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(jkyContribution, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#queryJkyScientificJobManagerManagerPageList(cn.qidisoft.edu.hzjt.model.JkyContribution,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<JkyContribution> queryJkyScientificJobManagerPageList(
      JkyContribution jkyContribution, Page page) {
    try {
      int c = jkyContributionMapper.queryScientificJobManagerPageCount(jkyContribution);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<JkyContribution> pageHolder =
          new PaginationBean<JkyContribution>(page, totalRecords);
      List<JkyContribution> resultList =
          jkyContributionMapper.queryScientificJobManagerPageList(jkyContribution,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryJkyScientificJobManagerManagerPageList [{}] error," + this.getClass().getName()
              + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(jkyContribution, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionService#exportScientificJobManagerPageList(cn.qidisoft.edu.hzjt.model.JkyContribution)
   */
  @Override
  public List<JkyContribution> exportScientificJobManagerPageList(JkyContribution jkyContribution) {
    try {
      return jkyContributionMapper.exportScientificJobManagerPageList(jkyContribution);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "exportScientificJobManagerPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(jkyContribution, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

}
